<?php

namespace Home\Controller;

use Think\Controller;
use Org\Util\Juhe;
/**
 * 进货单控制器
 * @author zhangwei
 */
class RejectController extends HomeController {
    public function reject() {
         $userid = session('userid');   
         $timestamp=time()-2*24*3600;
         $sql="select a.city,a.street,a.shopName,a.realName,a.phone,d.name,d.id,d.quantity,b.createDate,c.orderid,b.lastupdate from gr_buyers a,gr_order b,gr_order_goods c,gr_product d where a.id = $userid and b.orderid = c.orderid and c.pid = d.id and b.uid = a.id and b.lastupdate > $timestamp and  (b.status=2000 OR b.status=3000)";
         $res=D('buyers')->query($sql);
         $addr=$res[0]['city'].$res[0]['street'].$res[0]['shopname'];
         foreach($res as $key => $value){
             $value['createdate']= date('m-d',$value['createdate']);
             $res[$key]['xddate']=$value['createdate'];
         }
          $this->rejectinfo=$res;
          $this->assign("realname",$res[0]['realname']);
          $this->assign("phone",$res[0]['phone']);
          $this->assign("addr",$addr);
          $this->display();
    }
     public function saveReject() {
        $userid = session('userid');   
        $map = array('uid' => $userid);
        if (IS_POST) {
                // 上传文件 
                $setting = array(
                    'mimes' => '', //允许上传的文件MiMe类型
                    'maxSize' => 3 * 1024 * 1024, //上传的文件大小限制 (0-不做限制)
                    'exts' => 'jpg,gif,png,jpeg', //允许上传的文件后缀
                );
                /* 调用文件上传组件上传文件 */
                /*                 * 修改开始* */
                $pic_driver = C('PICTURE_UPLOAD_DRIVER');
                $upload = new \Think\Upload($setting, C('PICTURE_UPLOAD_DRIVER'), C("UPLOAD_{$pic_driver}_CONFIG"));
                $info = $upload->upload();
                if (!$info) {// 上传错误提示错误信息
                    $this->error($upload->getError());
                } else {// 上传成功}
                }
                    $reject_order="TD".time()."".rand(0, 10);
                    $user_name=$_POST['username'];
                    $user_tel=$_POST['usertel'];
                    $user_addr=$_POST['useraddr'];
                    $describe = $_POST['describe'];
                    $data = array('uid' => $userid,'orderid' => $reject_order, 'create_time'=>time(),'describe' => $describe,'user_name'=>$user_name,'user_tel'=>$user_tel,'user_addr'=>$user_addr,);
                    $reszsm=M('reject_order')->add($data);
                    if($reszsm){
                        $goods_length=count($_POST['good_name']);
                        $data2['orderid']=$reject_order;
                        for($i=0;$i<$goods_length;$i++){
                            $data2['pname']=$_POST['good_name'][$i];
                            $data2['old_order']=$_POST['good_order'][$i];
                            $data2['num']=$_POST['good_num'][$i];
                            $data2['pic']=$info[$i]["url"];
                           M('reject_order_goods')->add($data2);
                        }
                         $this->display();
                    }
            }
    }
}
